//
//  XMPhotosView.m
//  XMWeibo
//
//  Created by chi on 14-8-26.
//  Copyright (c) 2014年 xiaoming. All rights reserved.
//

#import "XMPhotosView.h"
#import "XMPhoto.h"
#import "UIImageView+WebCache.h"
#import "XMPhotoView.h"
#import "MJPhotoBrowser.h"
#import "MJPhoto.h"

@implementation XMPhotosView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        // 创建子控件
        [self setupSubviews];
        
        //设置属性
        self.backgroundColor = [UIColor clearColor];
        self.bounds = CGRectMake(0.0, 0.0, kThumbnailPhotoHW, kThumbnailPhotoHW);
        
        
    }
    return self;
}

/*** 手势识别 */
- (void)tap:(UITapGestureRecognizer*)recognize
{
    int count = _photos.count;
    
    NSInteger currentIdx = recognize.view.tag;
    
    // 1.封装图片数据
    NSMutableArray *photos = [NSMutableArray arrayWithCapacity:count];
    for (int i = 0; i<count; i++) {
        // 替换为中等尺寸图片
        XMPhoto *myPhoto = _photos[i];
        
        NSString *url = [myPhoto.thumbnail_pic stringByReplacingOccurrencesOfString:@"thumbnail" withString:@"bmiddle"];
//        NSString *url = [myPhoto.thumbnail_pic stringByReplacingOccurrencesOfString:@"thumbnail" withString:@"mw1024"];
        MJPhoto *photo = [[MJPhoto alloc] init];
        photo.url = [NSURL URLWithString:url]; // 图片路径
        photo.srcImageView = self.subviews[myPhoto.tag]; // 来源于哪个UIImageView
        [photos addObject:photo];
        
        if (count == 4 && ((UIView*)self.subviews[myPhoto.tag]).tag == recognize.view.tag) {
            currentIdx = recognize.view.tag;
            currentIdx = currentIdx > 2 ? currentIdx - 1 : currentIdx;
        }
        
    }
    
    // 2.显示相册
    MJPhotoBrowser *browser = [[MJPhotoBrowser alloc] init];
    browser.currentPhotoIndex = currentIdx; // 弹出相册时显示的第一张图片是？
    browser.photos = photos; // 设置所有的图片
    [browser show];
    

}

- (void)setPhotos:(NSArray *)photos
{
    _photos = photos;
    int i = 0;
    
    NSUInteger photoCount = photos.count;
    
    //4张图片时分两行显示
    if (photoCount == 4) {
        
        for (UIImageView *view in self.subviews) {
            view.hidden = YES;
        }
        
        for (; i < photoCount; ++i) {
            
            NSUInteger viewIdx = i;
            if (viewIdx > 1) {
                viewIdx++;
            }
            
            XMPhotoView *imageView = self.subviews[viewIdx];
            imageView.hidden = NO;
            XMPhoto *photo = photos[i];
            imageView.photo = photo;
            photo.tag = viewIdx;
//            [imageView sd_setImageWithURL:[NSURL URLWithString:photo.thumbnail_pic]];
        }
    }
    else {
        
        for (; i < photoCount; ++i) {
            XMPhotoView *imageView = self.subviews[i];
            imageView.hidden = NO;
            XMPhoto *photo = photos[i];
            imageView.photo = photo;
            photo.tag = i;
        }
        //其余图片不显示
        for (; i < 9; ++i) {
            ((UIImageView*)self.subviews[i]).hidden = YES;
        }
    }
    
    
    
}

#pragma mark - 创建子控件
- (void)setupSubviews
{
    for (int i = 0; i < 9; ++i) {
        XMPhotoView *imageView = [[XMPhotoView alloc]init];

        imageView.tag = i;
        
        //手势识别
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
        [imageView addGestureRecognizer:tap];
        
        [self addSubview:imageView];
    }
}

#pragma mark - 调整子控件位置
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat photoX = 0.0;
    CGFloat photoY = 0.0;
    
    NSUInteger subviewCount = self.subviews.count;
    
    for (int i = 0; i < subviewCount; ++i) {
        XMPhotoView *imageView = self.subviews[i];
        
        photoX = i % 3 * kThumbnailPhotoHW;
        photoY = i / 3 * kThumbnailPhotoHW;
        
        imageView.frame = CGRectMake(photoX, photoY, kThumbnailPhotoHW, kThumbnailPhotoHW);
        
    }

}


@end
